package 程序员面试金典;

import java.util.*;
import java.util.stream.Collectors;

public class 交换和 {

    public int[] findSwapValues(int[] array1, int[] array2) {
        //求和找差值
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < array2.length; i++) {
            set.add(array2[i]);
        }
        int sum1 = sum(array1);
        int sum2 = sum(array2);
        int res = sum1 - sum2;
        if (res==0 || res%2!=0)
            return new int[]{};
        for (int num : array1) {
            if (set.contains(num-res/2))
                return new int[]{num,num-res/2};
        }
        return new int[]{};
    }
    public int sum(int[] array){
        int sum=0;
        for (int i = 0; i < array.length; i++) {
            sum+=array[i];
        }
        return sum;
    }
}
